<?php

// 
//  getDerivs.php
//  fs-ger-morph
//  
//  Created on 2011-03-21.
// 
//	Script zum Sammeln von Nomenderivaten von
//	1. Wikipedia
//	2. IMSLex
//	3. NZZ
	

class getDerivs{

	var $derivate;

	function __construct(){
		$this->derivate = array();
	}
    
	// Wikipedia 
	function fromWikipedia(){
		$sitesCount = 100;
		
		require_once("wikiRetriever.php");
		$wRet = new wikiRetriever($sitesCount);
		
	    $articles = preg_split('/\s+/',$wRet->get());
		$words = array();
		foreach ($articles as $item) {
			if(!in_array(trim($item),$words))
				$words[] = trim($item);
		}
		unset($articles);
		
		$this->getDerivate($words,true);
		
		echo "got from wikipedia\n";
				
	}
	
	// IMSLex
	function fromIMSLex(){
		$words = file("../lexika/derivation/nounsIMSLex.list");
		$this->getDerivate($words);
		echo "got from IMSLex\n";
	}
	
	// NZZ
	function fromNZZ(){
		$words = file("../lexika/derivation/noncompounds_t.txt");
		$this->getDerivate($words);		
		echo "got from NZZ\n";
	}
	
	function getDerivate(&$words,$pl=false){		
		foreach($words as $word){
			if($pl)
				$word = preg_replace('/en$/','',$word);
			if($this->isDerivat(trim($word))){
				$this->derivate[] = trim($word);
			}
		}
	}
	
	function getPrintedList(){
		foreach($this->derivate as $derivat){
			echo $derivat."\n";
		}		
	}
	
	function writeList($path){
		$handle = fopen($path,"w");
		foreach($this->derivate as $derivat){
			fwrite($handle,$derivat."\n");
		}
		fclose($handle);
	}
	
	function isDerivat($word){
		
		$suffix = array("e", "(er)chen", "(er)ei", "(e)rich", "in", "(ig)keit", "heit", "land", "lein", "ling",
						"(n|l)er", "nis", "sal", "schaft" ,"sel", "tum", "ung");
		$ENlist = array("Bayern", "Bengalen", "Böhmen", "Franken", "Hessen", "Mähren", "Masuren", "Niedersachsen", "Ostpreussen", "Polen", "Pommern", "Preussen", "Preußen","Sachsen", "Schwaben", "Schweden", "Ungarn", "Vorpommern", "Westfalen");
		$IENlist = array("Abchasien", "Alexandrien", "Babylonien", "Baschkirien", "Bulgarien", "Etrurien", "Galatien", "Germanien", "Jordanien", "Jugoslawien", "Katalonien", "Kirgisien", "Kroatien", "Ozeanien", "Rumänien", "Ruthenien", "Sardinien", "Serbien", "Slowenien", "Tschechien", "Tschetschenien", "Turkmenien", "Wallonien");
		$ELlist = array("Dickwurzel", "Eichel", "Grundel", "Gründel", "Krustel", "Mistel", "Ribisel", "Rippeln", "Röteln" , "Schraubel", "Virgel", "Windel", "Wurzel", "Ärmel", "Bendel", "Brotkrümel", "Buchdeckel", "Dachsel", "Dächsel", "Dummbartel", "Fäustel", "Flügel", "Griffel", "Gürtel", "Hümpel", "Kiesel", "Knöchel", "Kokkel", "Kötel", "Kräpfel", "Kreisel", "Kringel", "Krümel", "Muddel", "Mündel", "Pardel", "Ränzel", "Ringel", "Rötel", "Säckel", "Schwertel", "Stengel", "Stichel", "Stöckel", "Tüpfel", "Wichtel", "Zügel", "Briesel", "Bündel", "Büschel", "Flössel", "Gesindel", "Gespöttel", "Hirtentäschel", "Kindel", "Krügel", "Küchel", "Kürzel", "Meringel", "Mündel", "Quartel", "Rändel", "Ränzel", "Rätzel", "Ritzel", "Rössel", "Schnitzel", "Schwertel", "Strohbündel", "Tüpfel", "Vesikel", "Würstel", "Zickel", "Bäuschel", "Deckel", "Dünkel", "Greuel", "Hebel", "Henkel", "Klöppel", "Kräuel", "Schlegel", "Schlägel", "Schlingel", "Stachel", "Schlüssel", "Senkel", "Sprengel", "Stössel", "Sturzel", "Stürzel", "Tippel", "Würfel", "Zwickel");
		// großgeschrieben, nur der Anfang?
		if(preg_match('/^[A-ZÖÄÜ].+/',$word)){
			// Derivats-Endung?
			if(preg_match('/('.implode('|',$suffix).')$/',$word)){
				return true;
			}
			if(in_array($word,$ENlist) || in_array($word,$IENlist)){
				return true;
			}
		}
		return false;
	}
		
	function clear(){
		$this->derivate = array();
	}
	
}
	
$test = new getDerivs();
$test->fromIMSLex();
$test->writeList("src/IMSLex.list");
$test->clear();
$test->fromWikipedia();
$test->writeList("src/wikipedia.list");
$test->clear();
$test->fromNZZ();
$test->writeList("src/nzz.list");

// echo "Deutung: ".$test->isDerivat('Deutung')."\n";
// echo "Bekenntnis: ".$test->isDerivat('Bekenntnis')."\n";
// echo "Ereignis: ".$test->isDerivat('Ereignis')."\n";
// echo "Würfel: ".$test->isDerivat('Würfel')."\n";
// echo "klein: ".$test->isDerivat('klein')."\n";
//  echo "Bettler: ".$test->isDerivat('Bettler')."\n";




	




?>